<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 30px;
	padding-bottom: 10px;
	}
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
   	.mytable td{
   	padding-top: 5px;
   	}
   .mytable .mytable-left{
   text-align: right;
   width : 100px;
   }
  .mytable .mytable-right{
   width : 150px;
   }
</style>
<script type="text/javascript">

var path = "${pageContext.request.contextPath}" + "/";
var maintool = null;
var searchPanel = null;
var searchField = null;
var searchButton = null;
var stockGrid = null;
var anaGrid = null;
var dialogAdd = null;
var dialogEdit = null;
var dialogDelete = null;
var validatorAdd = null;
var validatorEdit = null;

var searchUrl = path + "rm/filterQuery.html";
var addUrl = path + "rm/gridAdd.html";
var deleteUrl = path + "rm/gridDelete.html";
var editUrl = path + "rm/gridEdit.html";
var elmCodeUrl = path+"sm/omComboQuery.html?queryString=select chemicalId,chemicalId from SmTsChemical order by chemicalId";

$(function(){
	initVar();
	initLayout();
	initSearchPanel();
	initGrid();
	initComponent();
	initDialog();
	search();
});

function initVar(){
	maintool = $("#maintool");
	searchPanel = $("#search-panel");
	searchField = $("#search-field");
	searchButton = $("#search-button");
	stockGrid = $("#stockGrid");
	anaGrid = $("#anaGrid");
	dialogAdd = $("#dialog-Add");
	dialogDelete = $("#dialog-Delete");
	dialogEdit = $("#dialog-Edit");
}
function initLayout(){
	$('#page').omBorderLayout({
		panels : [ {
			id : "center-panel",
			title : "成分数据",
			region : "center"
		}, {
			id : "west-panel",
			title : "料仓数据",
			region : "west",
			resizable : true,
			width : '32%'
		}],
		spacing : 3
	});
	searchPanel.omPanel({
		title : "搜索",
		collapsible : true,
		height : 200
	});
}
function initSearchPanel(){
	$(':input',searchField).each(function() {
		$(this).keyup(function(event) {
			if (event.keyCode == 13) {
				search();
			}
		});
	});
	searchButton.omButton({
		icons:{left : path + 'resources/sm/images/search.png'},
		width:70,
		onClick:function(){
			search();
		}
	});
}
function initGrid(){
	stockGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:635,
		colModel : [
				{header : '料仓Id', name : 'storageId', align : 'center', width : '100', sort:'serverSide'},
				{header : '料仓名称', name : 'storageName', align : 'center', width : '190', sort:'serverSide'},
		            ],
        onRowClick : function(rowIndex, rowData, event) {
        	var group ={
        			op : "and",
        			rules : [{op : 'equal', field : 'storageId', value : rowData['storageId'], type : 'string'}]
        	};
        	var extraData = {
        			"domain" : "SmRmStorageElement",
        			"rules" : JSON.stringify(group),
        	};
        	anaGrid.omGrid({
        		dataSource : searchUrl,
        		extraData : extraData, 
        		method:'POST'
        	}, 'reload');
        }
	});
	
	anaGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:635,
		colModel : [
				{header : '元素名称', name : 'itemName', align : 'center', width : '70', sort:'serverSide'},
				{header : '元素成分值', name : 'itemValue', align : 'center', width : '70', sort:'serverSide'},
				{header : '时间戳', name : 'timeStamp', align : 'center', width : '160', sort:'serverSide'},
		            ],
        
	});
}
function initComponent(){
	$('#queryStorageType').omCombo({
		dataSource : [ {
			text : '高位料仓',
			value : 'B'
		}, {
			text : '高位料仓-手投',
			value : 'M'
		} , {
			text : '高位料仓-丝线',
			value : 'W'
		} , {
			text : '低位料仓-合金中间仓',
			value : 'ALLOY'
		} , {
			text : '低位料仓-镍库',
			value : 'NI'
		}, {
			text : '低位料仓-手投',
			value : 'MANUAL'
		}, {
			text : '低位料仓-丝线',
			value : 'WIRE'
		} ],
		width : 163,
	});
	
	var buttons = [];
	buttons.push({
		id : "add",
		label : "增加",
		onClick : function(){
			var selections = stockGrid.omGrid('getSelections', true);
			if (selections.length != 1) {
				$.omMessageBox.alert({
					content : '请选择料仓！',
				});
				return false;
			}
			showDialogAdd('新增', selections[0]);
		}
	});
	buttons.push({separtor : true});
	buttons.push({
		id : "delete",
		label : "删除",
		onClick : function(){
			var selections = anaGrid.omGrid('getSelections', true);
			if (selections.length != 1) {
				$.omMessageBox.alert({
					content : '请选择一行记录！',
				});
				return false;
			}
			showDialogDelete('删除', selections[0]);
		}
	});
	buttons.push({separtor : true});
	buttons.push({
		id : "edit",
		label : "修改",
		onClick : function(){
			var selections = anaGrid.omGrid('getSelections', true);
			if (selections.length != 1) {
				$.omMessageBox.alert({
					content : '请选择一行记录！',
				});
				return false;
			}
			showDialogEdit('修改', selections[0]);
		}
	});
	maintool.omButtonbar({btns:buttons});
	
}

function search()
{	
	var rules =[], i=0;
	$(':input',searchField).each(function() {
		if($.trim(this.name)!="")
			if( $(this).val() != null &&  $.trim($(this).val()) != "" 
					&& typeof($(this).attr('op')) != "undefined" && typeof($(this).attr('ty')) != "undefined")
				rules[i++] = {op : $(this).attr('op'), field : $(this).attr('name'), value : $(this).val(), type : $(this).attr('ty')};
	});
	var newValue = $('#queryStorageType',searchField).val();
	if(newValue!='' && newValue != 'B' && newValue != 'W' && newValue != 'M'){
		rules[i++] = {op : 'equal', field : 'storageLoc', value : $('#queryStorageType').val(), type : 'string'};
		rules[0] = {op : 'equal', field : 'storageType', value : 'U', type : 'string'};
	}
	var group ={
			op : "and",
			rules : rules
	};
	var extraData = {
			"domain" : "SmRmStorage",
			"rules" : i==0?null:JSON.stringify(group),
	};
	stockGrid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');
	
}
function initDialogAdd() {
	$('#itemName',dialogAdd).omCombo({
		dataSource : elmCodeUrl,
		width : 163
	});
	$('#itemValue',dialogAdd).omNumberField({allowDecimals : true});
	var buttons = [
			{
				text : "增加",
				click : function() {
					if(!validatorAdd.form()){
						$(':input','#addForm').each(function(){
							if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
								$(this).omTooltip('show');
							}
						});
						return;
					}
					var info = {
							"domain" : "SmRmStorageElement",
							"SmRmStorageElement" : getSubmitData(dialogAdd)
					};
					$.post( addUrl,info, 
							function(data, textStatus)
							{
						     if( data.success )
								{
						    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
						    	 dialogAdd.omDialog("close");
						    	 anaGrid.omGrid('reload');
								 
								}
						     else
						    	 {
									$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
						    	 }
					});
				}
			}, {
				text : "关闭",
				click : function() {
					$(':input','#addForm').each(function(){
						if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
							$(this).omTooltip('hide');
						}
					});
					dialogAdd.omDialog("close");
					anaGrid.omGrid('reload');
				}
			} ];
	dialogAdd.omDialog({
		width : 400,
		height : 250,
		autoOpen : false,
		modal : true,
		resizable : false,
		buttons : buttons,
		onClose : function(){
			$(':input','#addForm').each(function(){
				if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
					$(this).omTooltip('hide');
				}
			});
		}
	});
	showDialogAdd = function(title, rowData) {
		validatorAdd.resetForm();
		$('#addForm input').each(function(){
	    	$(this,showDialogAdd).val(rowData[this.name]);
		});
		dialogAdd.omDialog("option", "title", title);
		dialogAdd.omDialog("open");
	};

};

function initDialogDelete() {
	
	var buttons = [
			{
				text : "删除",
				click : function() {
					var info = {
							"domain" : "SmRmStorageElement",
							"ids" : $('#id',deleteForm).val()
					};
					$.post( deleteUrl,info, 
							function(data, textStatus)
							{
						     if( data.success )
								{
						    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
						    	 dialogDelete.omDialog("close");
						    	 anaGrid.omGrid('reload');
								 
								}
						     else
						    	 {
									$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
						    	 }
					});
				}
			}, {
				text : "关闭",
				click : function() {
					dialogDelete.omDialog("close");
					anaGrid.omGrid('reload');
				}
			} ];
	dialogDelete.omDialog({
		width : 400,
		height : 250,
		autoOpen : false,
		modal : true,
		resizable : false,
		buttons : buttons
	});
	showDialogDelete = function(title, rowData) {
		$('#deleteForm input').each(function(){
	    	$(this,showDialogDelete).val(rowData[this.name]);
		});
		dialogDelete.omDialog("option", "title", title);
		dialogDelete.omDialog("open");
	};

};

function initDialogEdit() {
	$('#itemValue',dialogEdit).omNumberField({allowDecimals : true});
	var buttons = [
			{
				text : "修改",
				click : function() {
					if(!validatorEdit.form()){
						$(':input','#editForm').each(function(){
							if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
								$(this).omTooltip('show');
							}
						});
						return;
					}
					var info = {
							"domain" : "SmRmStorageElement",
							"SmRmStorageElement" : getSubmitData(dialogEdit)
					};
					$.post( editUrl,info, 
							function(data, textStatus)
							{
						     if( data.success )
								{
						    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
						    	 dialogEdit.omDialog("close");
						    	 anaGrid.omGrid('reload');
								 
								}
						     else
						    	 {
									$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
						    	 }
					});
				}
			}, {
				text : "关闭",
				click : function() {
					$(':input','#editForm').each(function(){
						if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
							$(this).omTooltip('hide');
						}
					});
					dialogEdit.omDialog("close");
					anaGrid.omGrid('reload');
				}
			} ];
	dialogEdit.omDialog({
		width : 400,
		height : 250,
		autoOpen : false,
		modal : true,
		resizable : false,
		buttons : buttons,
		onClose : function(){
			$(':input','#editForm').each(function(){
				if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
					$(this).omTooltip('hide');
				}
			});
		}
	});

	showDialogEdit = function(title, rowData) {
		validatorEdit.resetForm();
		$('#editForm input').each(function(){
	    	$(this,showDialogEdit).val(rowData[this.name]);
		});
		dialogEdit.omDialog("option", "title", title);
		dialogEdit.omDialog("open");
	};

}
function getSubmitData(form){
	var info = {};
	$(':input',form).each(function(){
		if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
			info[$(this).attr('id')] = $(this).val();
		}
	});
	return JSON.stringify(info);
}
function initDialog(){
	initDialogAdd();
	initDialogDelete();
	initDialogEdit();
	validateAddInit();
	validateEditInit();
}

function validateAddInit()
{
	validatorAdd = $("#addForm").validate(
			{
				//编写验证规则
				rules: {
					itemName: { required: true },
					itemValue: { required: true, number: true },
					},
				messages: {
					itemName: { required: "" },
					itemValue: { required: "" },
					},
			});
	
	$('#itemName','#addForm').omTooltip({
		  region : 'right',
       html : '不能为空！',
       anchor : true ,
       offset : [-5,45]
	});
	$('#itemValue','#addForm').omTooltip({region : 'right', html : '非空，必须为数字，长度不超4位！', anchor : true , offset : [-5,5]});
}
function validateEditInit()
{
	validatorEdit = $("#editForm").validate(
			{
				//编写验证规则
				rules: {
					itemValue: { required: true, number: true },
					},
				messages: {
					itemValue: { required: "" },
					},
			});
	$('#itemValue','#editForm').omTooltip({region : 'right', html : '非空，必须为数字，长度不超4位！', anchor : true , offset : [-5,5]});
	
}
</script>
	
</head>
<body>
	<div id="page" style="height: 690px;">
		<div id="center-panel" >
			<div id="maintool"></div>
			<table id="anaGrid"></table>
		</div>
		<div id="west-panel" >
			<div id="search-panel"> 
				<div id="search-field">
				<table id="searchTable" class="mytable">
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓类型：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStorageType" name="storageType" op="equal" ty="string" type="text"/></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓编号：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStorageId" name="storageId" op="startwith" ty="string" type="text" /></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓名称：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStorageName" name="storageName" op="like" ty="string" type="text"/></span></td>
					</tr>
				</table>
				</div>		
				<div class="search-button"><span id="search-button">搜索</span></div>						
			</div>
			<table id="stockGrid"></table>
		</div>		
	</div>
	<div id="dialog-Add">	
		<form id="addForm">
		<table class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓编号:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="storageId" name="storageId" type="text" readonly="readonly"/></span></td>
			</tr>
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">元素名称:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="itemName" name="itemName" type="text" /></span></td>
			</tr>
			<tr>	
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">元素值:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="itemValue" name="itemValue" type="text" maxlength="4"/></span></td>
			</tr>
		</table>
		</form>	
	</div>
	<div id="dialog-Delete">
		<form id="deleteForm">
		<table class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓编号:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="storageId" name="storageId" type="text" readonly="readonly"/></span></td>
			</tr>
			<tr>	
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">元素名称:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="itemName" name="itemName" type="text" readonly="readonly"/></span></td>
			</tr>
			<tr>	
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">元素值:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="itemValue" name="itemValue" type="text" readonly="readonly"/></span></td>
			</tr>
		</table>
		<input id="id" name="id" type="hidden" />
		</form>
	</div>
	<div id="dialog-Edit">	
		<form id="editForm">
		<table class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓编号:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="storageId" name="storageId" type="text" readonly="readonly"/></span></td>
			</tr>
			<tr>	
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">元素名称:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="itemName" name="itemName" type="text" readonly="readonly"/></span></td>
			</tr>
			<tr>	
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">元素值:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="itemValue" name="itemValue" type="text" maxlength="4"/></span></td>
			</tr>
		</table>
		<input id="id" name="id" type="hidden" />
		<input id="timeStamp" name="timeStamp" type="hidden" />
		</form>	
	</div>
</body>
</html>